package me.lxw.blogsystem.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.lxw.blogsystem.dao.ArticleDAO;
import me.lxw.blogsystem.domain.Article;
import me.lxw.blogsystem.domain.Page;

@WebServlet("/category.do")
public class CategoryServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String categoryId = request.getParameter("id");
        // 1.从SiteDAO获取站点信息
        ArticleDAO articleDAO = new ArticleDAO();
        try {
            int pageNo = 0;
            String pageNoStr = request.getParameter("page");
            if (pageNoStr != null && !"".equals(pageNoStr)) {
                pageNo = Integer.valueOf(pageNoStr);
            }

            Page<Article> page = articleDAO.findByCategory(Integer.valueOf(categoryId), pageNo);

            List<Article> articleBannerList = articleDAO.findFeaturedFirst(4);
            // 2.将站点信息保存到请求域，
            request.setAttribute("page", page);
            request.setAttribute("categoryId", categoryId);
            request.setAttribute("articleBannerList", articleBannerList);
            // 3.请求转发到index.jsp
            request.getRequestDispatcher("/front/category.jsp").forward(request, response);
        } catch (SQLException e) {

            e.printStackTrace();
        }

    }

}
